package net.sashag.nadlandroid;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class ImageGridAdapter extends BaseAdapter {

	private Context context;
	private List<Bitmap> bitmaps = new ArrayList<Bitmap>();
	
	public ImageGridAdapter(Context ctx) {
		context = ctx;
	}
	
	public void addImage(Bitmap bitmap, boolean insertFirst /*LIFO/FIFO*/) {
		if (insertFirst) {
			bitmaps.add(0, bitmap);
		} else {
			bitmaps.add(bitmap);
		}
		notifyDataSetChanged();
	}
	
	public Bitmap getImage(int position) {
		return bitmaps.get(position);
	}
	
	public void removeImage(int position) {
		bitmaps.remove(position);
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return bitmaps.size();
	}

	@Override
	public Object getItem(int position) {
		return bitmaps.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView != null) {
			imageView = (ImageView)convertView;
		} else {
			imageView = new ImageView(context);
			imageView.setScaleType(ScaleType.CENTER_INSIDE);
			imageView.setPadding(8, 8, 8, 8);
		}
		imageView.setImageBitmap(bitmaps.get(position));
		return imageView;
	}


}
